import Vue from 'vue'
import { Toast, Actionsheet ,Swipe , SwipeItem } from 'mint-ui'
import App from './App.vue'

import router from './router'
import Axios from 'axios'
import VueAxios from 'vue-axios'
import Vuex from 'vuex'
import config from './config/config'

Vue.config.productionTip = false

// 使用vuex管理状态
Vue.use(Vuex)

Vue.component(Actionsheet.name, Actionsheet)
Vue.component(Swipe.name, Swipe)
Vue.component(SwipeItem.name, SwipeItem)
Vue.prototype.$baseurl = `${config.baseURL}wapc/`
//测试地址
// Vue.prototype.$baseurl = 'http://192.168.2.125:8080/#/'

const store = new Vuex.Store({
    state: {
        initMap: false, // 地图初始化完成
        isShowFoot: true,
        userInfo: {}, // 用户,路线id等信息
        isFinishRoute: false,
        goneLine: [], // 走过的路线
        currentPosition: [], // 当前位置
        paintLines: [], // 总线段长度
        lineData: {},
        curClassify: '', // 全局当前选中的分类
        leftMenuStatus: 1, // 菜单状态
        showDetailPop: false, // 是否显示详情弹框
        curScenicItem: {}, // 当前选中的点信息
        curScenicList: [], // 当前分类下的点
        userLocation: [], // 用户当前位置
        mapObj: {}, // 地图对象
        userLocationMarker: '', // 用户定位的marker集合
        scenicInfo: {}, // 景区信息
        mapMarkers: [], // 地图上存在的marker点
        markerClustererObj: {}, // 聚合点对象
        panoramaInfo: {}, // 全景图信息
        isWeiXin:null,//是否微信小程序环境
        isShowSpot:false//是否点击了搜索框里的选项，点击了设为true，然后轨迹那边监听isShowSpot的变化，点击搜索框里的选项则关闭轨迹显示
    },
    mutations: {
        setInitMap(state, val) {
            state.initMap = val;
        },
        setFoot(state, val) {
            state.isShowFoot = val;
        },
        setUserInfo(state, val) {
            state.userInfo = val;
        },
        setIsFinishRoute(state, val) {
            state.isFinishRoute = val;
        },
        setGoneLine(state, val) {
            state.goneLine = val;
        },
        setCurrentPosition(state, val) {
            state.currentPosition = val
        },
        setPaintLines(state, val) {
            state.paintLines = val
        },
        setLineData(state, val) {
            state.lineData = val
        },
        setCurClassify(state, val) {
            state.curClassify = val
        },
        setLeftMenuSts(state, val) {
            state.leftMenuStatus = val
        },
        setDetailPopupStatus(state, val) {
            state.showDetailPop = val
        },
        setCurScenicItem(state, val) {
            state.curScenicItem = val
        },
        setCurScenicList(state, val) {
            state.curScenicList = val
        },
        setUserLocation(state, val) {
            state.userLocation = val
        },
        setMapObj(state, val) {
            state.mapObj = val
        },
        setUserLocationMarker(state, val) {
            state.userLocationMarker = val
        },
        setScenicInfo(state, val) {
            state.scenicInfo = val
        },
        setMapMarkers(state, val) {
            state.mapMarkers.push(val)
        },
        setMarkerClustererObj(state, val) {
            state.markerClustererObj = val
        },
        setPanoramaInfo(state, val) {
            state.panoramaInfo = val
        },
        setIsWeiXin(state, val) {
            state.isWeiXin = val
        },
        setIsShowSpot(state, val) {
            state.isShowSpot = val
        }
    }
})

//判断是否是微信
// Vue.prototype.$isWeiXin = function() {
//               let ua = window.navigator.userAgent.toLowerCase()
//               let result=false
//               if (ua.match(/MicroMessenger/i) == 'micromessenger') {
//                 //   判断是微信环境，继续判断是否为小程序
//                 wx.miniProgram.getEnv((res)=>{
//                     console.log('res.miniprogram:',res.miniprogram)
//                     if (res.miniprogram) {
//                         result = true
//                     } else {
//                         result = false
//                     }
//                 })
//               } else {
//                 //非微信环境，返回false
//                 result = false
//               }

//               return result
//             },

// axios 拦截器
Axios.interceptors.request.use(
    config => {
        // 在发送请求之前做些什么
        return config
    },
    err => {
        // 对请求错误做些什么
        return Promise.reject(err)
    }
)

Axios.interceptors.response.use(
    res => {
        // eslint-disable-next-line
        console.log('success:', res)
        if (res.data.code === 200 || res.data.info == 'ok') {
            return res.data
        } else if (res.data.status == 0) {
            return res.data
        } else {
            Toast({
                message: res.data.msg
            })
        }
        // return res.data
    },
    err => {
        // eslint-disable-next-line
        console.log('fail:', err)
        return Promise.reject(err)
    }
)

// 使用axios
Vue.use(VueAxios, Axios)

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
